#include "tinyxml2.h"
#include <iostream>
#include <string>
#include <regex>
#include <vector>
#include <fstream>

using std::cout;
using std::endl;
using std::string;
using std::vector;
using std::ofstream;

using namespace tinyxml2;

struct RSSIteam
{
    string _title;
    string _url;
    string _content;
};

class RSS
{
public:
    RSS(size_t capa)
    {
        _rss.reserve(capa);
    }

    //读文件
    void read(const string &filename)
    {
        XMLDocument doc;
        doc.LoadFile(filename.c_str());
        if(doc.ErrorID())
        {
            std::cerr << "loadFile fail" << endl;
            return;
        }

        size_t idx = 0;

        XMLElement *itemNode = 
            doc.FirstChildElement("rss")->FirstChildElement("channel")->FirstChildElement("item");
        while(itemNode)
        {
            string title = itemNode->FirstChildElement("title")->GetText();
            /* cout << "title: " << title << endl; */
            string url=  itemNode->FirstChildElement("link")->GetText();
            /* cout << "url: " << url << endl; */
            // 如果没有content就用description代替
            string content;
            if(itemNode->FirstChildElement("content:encoded")){
                content = itemNode->FirstChildElement("content:encoded")->GetText();
            }
            else if(itemNode->FirstChildElement("content")){
                content = itemNode->FirstChildElement("content")->GetText();
            }
            else if(itemNode->FirstChildElement("description")){
                content = itemNode->FirstChildElement("description")->GetText();
            }
            else{// 没有内容，直接跳过这篇文章
                itemNode = itemNode->NextSiblingElement("item");
                continue;
            }


            std::regex reg("<[^>]+>");//通用正则表达式
            content = regex_replace(content, reg, "");
            /* cout << "content: " << content << endl; */
        
            RSSIteam rssItem;

            rssItem._title = title;
            rssItem._url = url;

            rssItem._content = content;

            _rss.push_back(rssItem);
            ++idx;

            itemNode = itemNode->NextSiblingElement("item");
        }
    }

    //写文件
    void store(const string& ripepagePath, const string& offsetPath)
    {
        ofstream ofs(ripepagePath);
        if(!ofs)
        {
            std::cerr << "open " << ripepagePath << " fail!" << endl;
            return;
        }

        ofstream ofsOffset(offsetPath);
        if(!ofsOffset)
        {
            std::cerr << "open " << offsetPath << " fail!" << endl;
            return;
        }

        size_t begin, end;
        for(size_t idx = 0; idx != _rss.size(); ++idx)
        {
            ofs << "<doc>" << endl 
                << "<docid>" << idx + 1 << "</docid>" << endl 
                << "<title>" << _rss[idx]._title << "</title>" << endl 
                << "<url>" << _rss[idx]._url << "</url>" << endl 
                << "<content>"; 
            // 写入ripepage.dat的同时设置偏移库offset.dat
            
            begin = ofs.tellp();
            ofsOffset << idx + 1 << " " << begin << " ";

            ofs << _rss[idx]._content; 

            end = ofs.tellp();
            ofsOffset << end - begin << endl;
            
            ofs << "</content>" << endl 
                << "</doc>" << endl;
            /* cout << ofs.tellp() << endl; */
        }

        ofs.close();
        ofsOffset.close();
    }
private:
    vector<RSSIteam> _rss;
};
